home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- #include "tk.h"
- #include "tkprivate.h"
- #include "tkmacglobals.h"
- #include "tkcolormaps.h"
-
- #if defined(__MWERKS__) || defined(__SC__)
- #include "console.h"
- #endif
-
- #if defined(__MWERKS__)
- #include "SIOUX.h"
- #endif
-
- static void DoInitManagers(void);
- static void DoInitConsole(void);
- static void DoSetUpMenus(void);
- static void DoInit(void);
- static void fillAttrib(GLenum type, GLint attrib[32]);
- static void CToPStr(char *cs, Str255 ps);
-
- static GLboolean tkInitApp = GL_TRUE;
- tkWindowRec tkWindow = { 30, 30, 200, 200, 0, NULL, NULL, GL_FALSE };
-
- float tkRGBMap[8][3] = {
- { 0, 0, 0 },
- { 1, 0, 0 },
- { 0, 1, 0 },
- { 1, 1, 0 },
- { 0, 0, 1 },
- { 1, 0, 1 },
- { 0, 1, 1 },
- { 1, 1, 1 }
- };
-
- static void DoInitManagers(void)
- {
- MaxApplZone();
- MoreMasters();
-
- InitGraf(&qd.thePort);
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
- }
-
- static void DoInitConsole(void)
- {
- #if defined(__MWERKS__)
- SIOUXSettings.initializeTB = FALSE;
- SIOUXSettings.standalone = FALSE;
- SIOUXSettings.setupmenus = FALSE;
- SIOUXSettings.autocloseonquit = TRUE;
- SIOUXSettings.asktosaveonclose = TRUE;
- SIOUXSettings.showstatusline = TRUE;
- SIOUXSettings.userwindowtitle = NULL;
- SIOUXSettings.tabspaces = 4;
- SIOUXSettings.columns = 60;
- SIOUXSettings.rows = 8;
- SIOUXSettings.toppixel = 100;
- SIOUXSettings.leftpixel = 100;
- SIOUXSettings.fontid = kFontIDMonaco;
- SIOUXSettings.fontsize = 9;
- SIOUXSettings.fontface = normal;
- SIOUXSettings.enabledraganddrop = FALSE;
- SIOUXSettings.outlinehilite = FALSE;
- SIOUXSettings.wasteusetempmemory = TRUE;
- #endif
- }
-
- static void DoSetUpMenus(void)
- {
- Handle menuBar;
-
- menuBar = GetNewMBar(rMenuBar);
- if(!menuBar) printf("TK error: MenuBar not found.\n");
-
- SetMenuBar(menuBar);
- DisposeHandle(menuBar);
- AppendResMenu(GetMenuHandle(mApple),'DRVR');
- DrawMenuBar();
- }
-
- static void DoInit(void)
- {
- DoInitConsole();
- DoInitManagers();
- DoSetUpMenus();
-
- gDone = false;
- }
-
- void tkCloseWindow(void)
- {
- if(tkWindow.context) aglDestroyContext(tkWindow.context);
- if(tkWindow.drawable) DisposeWindow((GrafPort *) tkWindow.drawable);
- if(tkWindow.fmt) aglDestroyPixelFormat(tkWindow.fmt);
-
- tkWindow.x = 30;
- tkWindow.y = 30;
- tkWindow.width = 200;
- tkWindow.height = 200;
- tkWindow.type = 0;
- tkWindow.context = NULL;
- tkWindow.drawable = NULL;
- tkWindow.fmt = NULL;
- tkWindow.double_buffer = GL_FALSE;
- }
-
- void tkQuit(void)
- {
- gDone = TRUE;
- tkCloseWindow();
- }
-
- void tkInitDisplayMode(GLenum type)
- {
- tkWindow.type = type;
- }
-
- void tkInitPosition(int x, int y, int width, int height)
- {
- tkWindow.x = x;
- tkWindow.y = y;
- tkWindow.width = width;
- tkWindow.height = height;
- }
-
- void tkSwapBuffers(void)
- {
- if(!tkWindow.drawable && !tkWindow.fullscreen) printf("TK error: no window defined\n");
-
- if(tkWindow.double_buffer) aglSwapBuffers(aglGetCurrentContext());
- }
-
- static void fillAttrib(GLenum type, GLint attrib[32])
- {
- GLushort i = 0;
-
- if (!(type & TK_INDEX))
- {
- attrib[i++] = AGL_RGBA;
- }
-
- if ((type & TK_DOUBLE))
- {
- attrib[i++] = AGL_DOUBLEBUFFER;
- tkWindow.double_buffer = true;
- }
- else
- {
- tkWindow.double_buffer = false;
- }
-
- if (type & TK_OVERLAY)
- {
- attrib[i++] = AGL_LEVEL;
- attrib[i++] = 0;
- }
-
- if (type & TK_UNDERLAY)
- {
- attrib[i++] = AGL_LEVEL;
- attrib[i++] = 0;
- }
-
- if (type & TK_STENCIL)
- {
- attrib[i++] = AGL_STENCIL_SIZE;
- attrib[i++] = 1;
- }
-
- if (type & TK_DEPTH)
- {
- attrib[i++] = AGL_DEPTH_SIZE;
- attrib[i++] = 24;
- }
-
- if (type & TK_ACCUM)
- {
- attrib[i++] = AGL_ACCUM_RED_SIZE;
- attrib[i++] = 16;
- attrib[i++] = AGL_ACCUM_GREEN_SIZE;
- attrib[i++] = 16;
- attrib[i++] = AGL_ACCUM_BLUE_SIZE;
- attrib[i++] = 16;
-
- if(type & TK_ALPHA)
- {
- attrib[i++] = AGL_ACCUM_ALPHA_SIZE;
- attrib[i++] = 16;
- }
- }
-
- if (type & TK_ALPHA)
- {
- attrib[i++] = AGL_ALPHA_SIZE;
- attrib[i++] = 1;
- }
-
- if (type & TK_AUX)
- {
- attrib[i++] = AGL_AUX_BUFFERS;
- attrib[i++] = 1;
- }
-
- if (type & TK_FULLSCREEN)
- {
- attrib[i++] = AGL_FULLSCREEN;
- }
-
- attrib[i++] = AGL_ALL_RENDERERS;
-
- attrib[i++] = 0;
- }
-
- void CToPStr(char *cs, Str255 ps)
- {
- GLint i, l;
-
- l = strlen(cs);
- if(l > 255) l = 255;
- ps[0] = l;
- for(i = 0; i < l; i++) ps[i + 1] = cs[i];
- }
-
- GLenum tkInitWindow(char *title)
- {
- GLint attrib[32];
- Str255 pstr;
- GLint value;
-
- /* Check if a window is already open */
- if(tkWindow.drawable)
- {
- printf("TK error: window already defined\n");
- exit(1);
- }
-
- /* Init application once */
- if(tkInitApp)
- {
- DoInit();
- tkInitApp = GL_FALSE;
- }
-
- /* Read attribute list */
- fillAttrib(tkWindow.type, attrib);
-
- /* Choose pixel format */
- tkWindow.fmt = aglChoosePixelFormat(NULL, 0, attrib);
- if(!tkWindow.fmt)
- {
- printf("TK error: Pixel format could not be achieved\n");
- exit(1);
- }
-
- aglDescribePixelFormat(tkWindow.fmt, AGL_FULLSCREEN, &value);
- tkWindow.fullscreen = value;
-
- /* Create context */
- tkWindow.context = aglCreateContext(tkWindow.fmt, NULL);
- if(!tkWindow.context)
- {
- printf("TK error: Context could not be created\n");
- exit(1);
- }
-
- /* Make context current */
- if(!tkWindow.fullscreen)
- {
- /* Create window */
- tkWindow.drawable = (AGLDrawable) GetNewCWindow(kMainWindow,nil,(WindowPtr)-1L);
- if(!tkWindow.drawable)
- {
- printf("TK error: window could not be created\n");
- exit(1);
- }
-
- CToPStr(title, pstr);
- SetWTitle((GrafPort *) tkWindow.drawable, pstr);
- SizeWindow((GrafPort *) tkWindow.drawable,tkWindow.width,tkWindow.height,GL_FALSE);
- MoveWindow((GrafPort *) tkWindow.drawable,tkWindow.x,tkWindow.y,GL_FALSE);
- ShowWindow((GrafPort *) tkWindow.drawable);
- HiliteWindow((GrafPort *) tkWindow.drawable, TRUE);
- SetPort((GrafPort *) tkWindow.drawable);
-
- if(!aglSetDrawable(tkWindow.context, tkWindow.drawable))
- {
- printf("TK error: Could not attach to context\n");
- exit(1);
- }
- }
- else
- {
- if(!aglSetFullScreen(tkWindow.context, tkWindow.width, tkWindow.height, 60, 0))
- {
- printf("TK error: Could not attach to context\n");
- exit(1);
- }
-
- tkWindow.x = 0;
- tkWindow.y = 0;
-
- tkWindow.drawable = NULL;
- }
-
- if(!aglSetCurrentContext(tkWindow.context))
- {
- printf("TK error: Could not attach to context\n");
- exit(1);
- }
-
- return GL_TRUE;
- }
-
- AGLPixelFormat tkGetPixelFmt(void)
- {
- return tkWindow.fmt;
- }